package com.ask.forMe.service.impl;

import com.ask.forMe.mapper.OrderMapper;
import com.ask.forMe.model.entity.Order;
import com.ask.forMe.model.entity.Result;
import com.ask.forMe.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    /**
     * 处理超时订单
     */
    @Override
    public void processTimeoutOrders() {

    }

    /**
     * 生成订单号
     *
     * @return
     */
    @Override
    public String createOrderNo() {
        return "order:123...";
    }

    /**
     * 根据订单号查询订单
     *
     * @param orderNum
     * @return
     */
    @Override
    public Order queryByOrderNum(String orderNum) {
        List<Order> orderList = query().eq("order_no", orderNum).list();
        log.info("查询订单信息，订单号：{}", orderNum);
        if (orderList == null || orderList.isEmpty())
            return null;
        return orderList.get(0);
    }

    /**
     * 取消订单
     *
     * @param orderNum
     * @return
     */
    @Override
    public Result cancelOrder(String orderNum) {
        List<Order> orderList = query().eq("order_no", orderNum).list();
        if (orderList == null || orderList.isEmpty())
            return Result.fail("订单不存在！");
        Order order = orderList.get(0);
        order.setStatus(Order.STATUS_CANCEL);
        updateById(order);
        log.info("取消订单：{}", order.getOrderNo());
        // todo 调用库存扣减服务
        return Result.success("订单取消成功！");
    }
}
